package com.thwj.project.app.event;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.thwj.common.utils.StringUtils;
import com.thwj.project.biz.domain.Dynamic;
import com.thwj.project.biz.enums.AuditStatusEnum;
import com.thwj.project.biz.service.IDynamicService;
import com.thwj.project.secCheck.SecCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class DynamicEventConsumer {

    @Autowired
    SecCheckUtil secCheckUtil;
    @Autowired
    IDynamicService dynamicService;



    @Async
    @EventListener
    public void  onDynamicOperateEvent(DynamicOperateEvent event) {
        switch (event.getOperateType()){
            case PUBLISH:
                doPublish(event.getDynamic());
                break;
            case LOOK:
                doLook(event.getDynamic());
                break;
            case GOOD:
                doGood(event.getDynamic());
                break;
            case CANCEL_GOOD:
                doCancelGood(event.getDynamic());
                break;
            case COMMENT:
                doComment(event.getDynamic());
                break;
            case DELETE_COMMENT:
                doDeleteComment(event.getDynamic());
                break;

        }

    }

    private void doDeleteComment(Dynamic dynamic) {
        // 无脑 更新+1  即可
        dynamicService.update(new UpdateWrapper<Dynamic>()
                .setSql("comment_count = comment_count - 1")
                .eq("id", dynamic.getId())
        );
    }

    private void doComment(Dynamic dynamic) {
        // 无脑 更新+1  即可
        dynamicService.update(new UpdateWrapper<Dynamic>()
                .setSql("comment_count = comment_count + 1")
                .eq("id", dynamic.getId())
        );
    }

    private void doCancelGood(Dynamic dynamic) {
        // 无脑 更新+1  即可
        dynamicService.update(new UpdateWrapper<Dynamic>()
                .setSql("good_count = good_count - 1")
                .eq("id", dynamic.getId())
        );
    }

    private void doGood(Dynamic dynamic) {
        // 无脑 更新+1  即可
        dynamicService.update(new UpdateWrapper<Dynamic>()
                .setSql("good_count = good_count + 1")
                .eq("id", dynamic.getId())
        );
    }

    private void doLook(Dynamic dynamic) {
        // 无脑 更新+1  即可
        dynamicService.update(new UpdateWrapper<Dynamic>()
                .setSql("look_count = look_count + 1")
                .eq("id", dynamic.getId())
        );
    }


    private void doPublish(Dynamic dynamic) {
        //这里要先查看 如果发现该动态已经标签失败 那这个动态一定是在前置有什么问题 我们直接不进行审核 保持谨慎的态度  维持失败
        if (AuditStatusEnum.auditing_fail.getValue() == dynamic.getStatus()) {
            return;
        }
        // 文字审核
        if (StringUtils.isNotEmpty(dynamic.getContent())) {
            boolean checkContent = secCheckUtil.checkContentSecurity(dynamic.getContent());
            if(!checkContent) {
                dynamicService.update(new UpdateWrapper<Dynamic>()
                        .set("status", AuditStatusEnum.auditing_fail.getValue())
                        .set("error", "文字审核不通过")
                        .eq("id", dynamic.getId())
                );
                return;
            }
        }
        if (StringUtils.isNotEmpty(dynamic.getPicList())) {
            List<String> picList = Arrays.asList(dynamic.getPicList().split(","));
            // 图片审核 这里考虑多线程处理 todo
            for (String picUrl : picList) {
                boolean checkPic = secCheckUtil.checkImageSecurity(picUrl);
                if(!checkPic) {
                    dynamicService.update(new UpdateWrapper<Dynamic>()
                            .set("status", AuditStatusEnum.auditing_fail.getValue())
                            .set("error", picUrl + " 该图片审核不通过")
                            .eq("id", dynamic.getId())
                    );
                    break;
                }
            }
        }
        // 上面检查没有问题则直接更新 成功
        dynamicService.update(new UpdateWrapper<Dynamic>()
                .set("status", AuditStatusEnum.auditing_success.getValue())
                .eq("id", dynamic.getId())
        );
    }


}
